package light.head.controller.oa;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.GoodsDAO;
import light.head.dao.oa.GoodsTypeDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Goods;
import light.head.model.GoodsType;
import light.head.model.Organ;
/***
 * 物品信息
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_GOODS)
public class GoodsController extends SupporterController{
	
	private GoodsDAO gDAO;
	private OrganDAO organDAO;
	private GoodsTypeDAO gtDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.GOODS_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.ENAME, Parm.DELETED, Parm.OC, Parm.SORT, Parm.GTC);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (gDAO.findByMap(organDAO.getSonCodeList(oc), map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.GOODS_LIST);
	}

	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Goods goods = gDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.GOODS, goods);
		}
		return new ModelAndView(Url.GOODS_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(Goods g, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (g == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		if (g.getCode() == null) {
			Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
			List<Goods> gs = gDAO.findByNameOc(g.getName(), organDAO.getSonCodeList(oc));
			if (gs != null && gs.size() > 0)  {
				print(response, Msg.EXISTS);
				return;
			}
			g.setOc(cu_organCode(request));
			g.setCu(cu_code(request));
			g.setCt(new Date());
			g.setDeleted(false);
			result = gDAO.add(g);
		} else {
			result = gDAO.update(g);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	  
	@RequestMapping(value = Url.UPDATE)
	public void update(Goods g, HttpServletRequest request, HttpServletResponse response) throws Exception{
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		g =gDAO.get(Integer.parseInt(code));
		if (g == null) {
			print(response, Msg.NOT_FIND);
			return;
		}
		if (gDAO.upd(g) == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	
	@RequestMapping(value = Url.DELETE)
	public void delete(Goods g, HttpServletRequest request, HttpServletResponse response) throws Exception{
		String code = request.getParameter(Parm.CODE);
		if (code == null) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		if (gDAO.del(Integer.parseInt(code)) == RESULT.SUCCESS)
			print(response,Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Goods g = gDAO.get(Integer.parseInt(code));
		if (null == g) {
			print(response, Msg.NOT_FIND);
			return;
		}
		g.setDeleted(true);
		if (gDAO.upd(g) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Goods g = gDAO.get(Integer.parseInt(code));
		if (null == g) {
			print(response, Msg.NOT_FIND);
			return;
		}
		g.setDeleted(false);
		if (gDAO.upd(g) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(Url.AID_INIT)
	public ModelAndView aidQueryList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer code = organDAO.getRoot(cu_organCode(request)).getCode();
 		if (null != code) {
 			List<Organ> list = organDAO.getAll();
			Organ o = findOgranByCode(code, list);
			if (null != o) {
				StringBuffer tree = new StringBuffer();
				if (!o.getDeleted()) {
					tree.append("p.add('" + o.getCode() + "','-1','"+ o.getName() + "');");
					aidTree(tree, o.getSonList());
				}
			
			request.setAttribute(Parm.TREE, tree);
			request.setAttribute(Parm.ORGAN, list);
			}
 		}
 		request.setAttribute(Parm.ID, request.getParameter(Parm.ID));
		request.setAttribute(Parm.NAME, request.getParameter(Parm.NAME));
		return new ModelAndView(Url.GOODS_AID_INIT);
	}
	
	private Organ findOgranByCode(Integer code, List<Organ> list){
		if(null != code && null != list && 0 < list.size()){
			for (Organ o : list){
				if(code.intValue() == o.getCode().intValue()){
					return o;
				} else {
					Organ or = findOgranByCode(code, o.getSonList());
					if (or != null) {
						return or;
					}
				}
			}
		}
		return null;
	}
	
	private void aidTree(StringBuffer tree, List<Organ> list) {
		if (null == tree || null == list || list.size() == 0)
			return;
		for (Organ o : list) {
			tree.append("p.add('" + o.getCode() + "','" + o.getPc() + "','" + o.getName() + "');");
			aidTree(tree, o.getSonList());
		}
	}

	
	/**
	 * 查询物品类型
	 */
	@RequestMapping("type")
	public ModelAndView TypeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		List<GoodsType> gtelist = gtDAO.getAll(oc);
		if (null != gtelist && gtelist.size() > 0) {
			StringBuffer tree = new StringBuffer();
			for (GoodsType gte : gtelist) {
				if (null != gte.getDeleted()) {
					tree.append("p.add('" + gte.getCode() + "','-1','" + gte.getName() + "');");
					TypeTree(tree, gte.getSonList());
				}
				request.setAttribute(Parm.TREE, tree);
				request.setAttribute(Parm.GOODSTYPE, gte);
			}
		}
 		request.setAttribute(Parm.ID, request.getParameter(Parm.ID));
		request.setAttribute(Parm.NAME, request.getParameter(Parm.NAME));
		return new ModelAndView(Url.GOODS_TYPE);
	}
	
	
	private void TypeTree(StringBuffer tree, List<GoodsType> list) {
		if (null == tree || null == list || list.size() == 0)
			return;
		for (GoodsType gt : list) {
			if (!gt.getDeleted()) {
				tree.append("p.add('" + gt.getCode() + "','" + gt.getPc() + "','" + gt.getName() + "');");
				TypeTree(tree, gt.getSonList());
			}
		}
	}
	
	@Autowired
	public void setgDAO(GoodsDAO gDAO) {
		this.gDAO = gDAO;
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}

	@Autowired
	public void setGtDAO(GoodsTypeDAO gtDAO) {
		this.gtDAO = gtDAO;
	}
	

}
